{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 五年人口迁徙图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "import pandas as pd\n",
    "import requests as rq\n",
    "from iclientpy import viz"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用pandas读取数据，但是列错乱，修正后生成新的dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sdf = pd.read_html(rq.get('http://www.stats.gov.cn/tjsj/pcsj/rkpc/6rp/html/B0708.htm', headers={'User-agent': 'Mozilla/5.0'}).content)[0]\n",
    "df=sdf.iloc[8:39,[0]+list(range(3,34))]\n",
    "df.columns=['现住地']+sdf.iloc[5,1:32].tolist()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从源数据获取各地方迁出总人口的dataframe，然后提取迁出人口最多的五个省份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "tdf=sdf.iloc[7:8,3:34]\n",
    "tdf.columns=sdf.iloc[5,1:32].tolist()\n",
    "tdf.loc[7]=pd.to_numeric(tdf.loc[7])\n",
    "ttdf=tdf.T\n",
    "ttdf.columns=['value']\n",
    "sttdf=ttdf.sort_values(by='value',ascending=False)\n",
    "top5_name=sttdf.iloc[0:5].index.tolist()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构建geolines所需要的数据格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_data(dataframe):\n",
    "    alldata=[]\n",
    "    for index,series in dataframe.iteritems():\n",
    "        if index !=  \"现住地\":\n",
    "            single_data=[]\n",
    "            for ind,value in series.iteritems():\n",
    "                if  index != re.sub('\\s','',dataframe['现住地'][ind]):\n",
    "                   single_data.append([{'name': index},{'name': re.sub('\\s','',dataframe['现住地'][ind]),'value':value}]) \n",
    "            alldata.append(single_data)\n",
    "    return alldata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data=get_data(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "生成迁徙图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "chart=viz.geolines(data=data,address_key='name',value_key='value',names=df.columns[1:].tolist(),max_symbolsize=15,min_symbolsize=5,symbol='arrow',selected_mode='multiple',selected_legend=top5_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "展示迁徙图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2664651ab8c44536a785015da868d400",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(basemap={'url': 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', 'max_zoom': 19, 'attribution': '…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "chart"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "迁徙出去人口的等级符号专题图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "79c69b3978bb40d7b5e8cd7dfeab1c12",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(basemap={'url': 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', 'max_zoom': 19, 'attribution': '…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ttdf['地区']=ttdf.index.tolist()\n",
    "out_chart=viz.ranksymboltheme(data=ttdf,address_key='地区',value_key='value',is_over_lay=False,codomain=(0,400000),fill_opacity=0.5)\n",
    "out_chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ba82c7fa978b4f5cb5e7f9d518d0ff99",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(IntRangeSlider(value=(6000, 400000), continuous_update=False, description='值域范围:', layout=Layou…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "out_chart.interact()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算各个地区迁入人口的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "idf=sdf.iloc[8:39,3:34]\n",
    "idf=idf.apply(pd.to_numeric, errors='ignore')\n",
    "idf=idf.fillna(0)\n",
    "itdf=idf.T.apply(sum)\n",
    "itdf.columns=sdf.iloc[5,1:32].tolist()\n",
    "idf=pd.DataFrame([sdf.iloc[5,1:32].tolist(),itdf.data.tolist()]).T\n",
    "idf.columns=['地区','value']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "各个地区迁入人口的等级符号专题图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2e45d10b56ac438da46b3ebbadc6ee91",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(basemap={'url': 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', 'max_zoom': 19, 'attribution': '…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "in_chart=viz.ranksymboltheme(data=idf,address_key='地区',value_key='value',is_over_lay=False,codomain=(0,400000))\n",
    "in_chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "84be1d79d2ed441d896f7711bbe81c3e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(IntRangeSlider(value=(9000, 400000), continuous_update=False, description='值域范围:', layout=Layou…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "in_chart.interact()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
